package Service;

import DAO.StockDAO;
import DAO.StockInfoDAO;
import Utils.GetStockInfo;
import Utils.Stock;
import Utils.StockInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class StockService {
    @Autowired
    private StockDAO stockDao;
    @Autowired
    private StockInfoDAO stockInfoDao;

    public void UpdateStock() {
        ArrayList<Stock> StockList = GetStockInfo.getStock();
        stockDao.UpdateBatchStock(StockList);
    }

    public void UpdateStock(ArrayList<Stock> StockList) {
        stockDao.UpdateBatchStock(StockList);
    }

    public ArrayList<Stock> getAllStock() {
        ArrayList<Stock> StockList = new ArrayList<>();
        try{
            StockList = stockDao.GetAllStock();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            return StockList;
        }
    }

    public void UpdateStockInfo() {
        ArrayList<StockInfo> StockInfoList = GetStockInfo.getStockInfo();
        stockInfoDao.UpdateStockInfo(StockInfoList);
    }

    public ArrayList<StockInfo> getAllStockInfo()
    {
        return stockInfoDao.getALLStockInfo();
    }
    //清空表
    public void TruncateStockInfo()
    {
        stockInfoDao.TruncateStockInfo();
    }
    //根据StockCode查询股票
    public Stock GetStockByStockCode(String StockCode)
    {
        Stock stock = null;

        try {
            stock = stockDao.GetStockByStockCode(StockCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            return stock;
        }
    }
    //根据StockCode查询股票信息
    public StockInfo getStockInfoByCode(String StockCode)
    {
        StockInfo stockinfo = null;

        try {
            stockinfo = stockInfoDao.getStockInfoByCode(StockCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            return stockinfo;
        }
    }
    //分组查询
    public ArrayList<StockInfo> getAllStockInfoLimit(int start,int PageSize)
    {
        ArrayList<StockInfo> stockINfos = null;
        try {
            stockINfos = stockInfoDao.getAllStockInfoLimit(start, PageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            return stockINfos;
        }
    }
    //获取记录个数
    public Integer getStockInfoCount()
    {
        Integer count = null;
        try {
            count = stockInfoDao.getStockInfoCount();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            return count;
        }
    }
}
